/**
 * 
 */
package com.sad.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;



/**
 * @author zhaoqy
 *
 */
public final class DateUtils {

	public static final DateFormat formaterPoint = new SimpleDateFormat("yyyy.MM.dd");

	/**
	 * 返回给定的日期date距离day天得日期，-1表示前一天，+1表示后一天
	 * 
	 * @param date
	 * @param day
	 * @return
	 */
	public static Calendar calculation(Timestamp date, int day) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, day);

		return cal;
	}

	/**
	 * 返回给定的日期date距离day天得日期，以yyyy.MM.dd格式化
	 * <p>
	 * -1表示前一天，+1表示后一天
	 * 
	 * @param date
	 * @param day
	 * @return
	 */
	public static String calculationFormat(Timestamp date, int day) {
		Calendar cal = calculation(date, day);
		return formaterPoint.format(cal.getTime());
	}

	/**
	 * 返回给定的日期date距离day天得日期，以给定的pattern格式化
	 * <p>
	 * -1表示前一天，+1表示后一天
	 * 
	 * @param date
	 * @param day
	 * @return
	 */
	public static String calculationFormat(Timestamp date, int day, String pattern) {
		Calendar cal = calculation(date, day);
		DateFormat formater = new SimpleDateFormat(pattern);
		return formater.format(cal.getTime());
	}

	/**
	 * 获取给定日期的上个月的最后一天
	 * 
	 * @param date
	 * @return
	 */
	public static String getPreMonthLastDay(Timestamp date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int month = calendar.get(Calendar.MONTH);
		calendar.set(Calendar.MONTH, month - 1);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		Date dateForLastMonth = calendar.getTime();
		return sdf.format(dateForLastMonth);
	}

	/**
	 * 获取某月的第一天
	 * 
	 * @param date
	 * @return
	 */
	public static String getMonthFirstDay(Timestamp date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, 0);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		String first = formaterPoint.format(cal.getTime());
		return first;
	}

	/**
	 * 获取给定的日期所在周的第一天和最后一天
	 * 
	 * @param date
	 * @return
	 */
	public static Timestamp[] getWeekFirstAndLastDay(Timestamp date) {

		Timestamp[] fe = new Timestamp[2];
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);

		// 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
		if (1 == dayWeek) {
			cal.add(Calendar.DAY_OF_MONTH, -1);
		}

		cal.setFirstDayOfWeek(Calendar.MONDAY);// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
		int day = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
		cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
		fe[0] = new Timestamp(cal.getTime().getTime());

		cal.add(Calendar.DATE, 6);
		fe[1] = new Timestamp(cal.getTime().getTime());

		return fe;
	}

	/**
	 * 获取给定的日期所在月的第一天和最后一天
	 * 
	 * @param date
	 * @return
	 */
	public static Timestamp[] getMonthFirstAndLastDay(Timestamp date) {

		Timestamp[] fe = new Timestamp[2];
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
		fe[0] = new Timestamp(cal.getTime().getTime());

		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		fe[1] = new Timestamp(cal.getTime().getTime());

		return fe;
	}

	/**
	 * 计算两个日期之间相差的天数
	 * 
	 * @param smdate
	 *            较小的时间
	 * @param bdate
	 *            较大的时间
	 * @return 相差天数
	 * @throws ParseException
	 */
	public static int daysBetween(Timestamp smdate, Timestamp bdate) {
		// SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		// smdate=sdf.parse(sdf.format(smdate));
		// bdate=sdf.parse(sdf.format(bdate));
		// Calendar cal = Calendar.getInstance();
		// cal.setTime(smdate);
		// long time1 = cal.getTimeInMillis();
		// cal.setTime(bdate);
		// long time2 = cal.getTimeInMillis();
		long between_days = (smdate.getTime() - bdate.getTime()) / (1000 * 3600 * 24);
		return Integer.parseInt(String.valueOf(between_days));
	}

	public static void main(String[] args){
		System.out.println(showTime(new Date(Timestamp.valueOf("2015-04-15 23:29:47").getTime()),null));
	}
	
	public static String showTime(Date ctime, String format) {
		String r = "";
		if (ctime == null)
			return r;
		if (format == null)
			format = "yyyy-MM-dd HH:mm:ss";

		long nowtimelong = System.currentTimeMillis();
		long ctimelong = ctime.getTime();
		long result = Math.abs(nowtimelong - ctimelong);

		// 一分钟内
		if (result < 60000){
			long seconds = result / 1000;
			r = seconds + "秒";
			// 一小时内
		} else if (result >= 60000 && result < 3600000){
			long seconds = result / 60000;
			r = seconds + "分";
			// 一天内
		} else if (result >= 3600000 && result < 86400000){
			long seconds = result / 3600000;
			r = seconds + "小时";
			// 日期格式
		} else{
			long seconds = result / 3600000 /24;
			r = seconds + "天";
		}
		return r;
	}

}
